package com.controller;

import com.alibaba.fastjson.JSON;
import com.service.SugerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @Description: TODO QQ1667847363
 * @author: xiao kun tai
 * @date:2022/1/14 9:41
 */

@RestController
@RequestMapping("/api/suger")
public class SugarController {

    @Autowired
    private SugerService sugerService;


    @RequestMapping("/gmv")
    public String getGmv(@RequestParam(value = "date", defaultValue = "0") int date) {
        if (date == 0) {
            date = getToday();

        }

//        BigDecimal gmv = sugerService.getGmv(date);

        HashMap<String, Object> result = new HashMap<>();
        result.put("status", 0);
        result.put("msg", "");
        result.put("data", 1201025);
        /**
         * {
         *   "status": 0,
         *   "msg": "",
         *   "data": 1201025.9922492304
         * }
         */
        return JSON.toJSONString(result);
    }

    /**
     * {
     * "status": 0,
     * "msg": "",
     * "data": {
     * "categories": [
     * "苹果",
     * "三星",
     * "华为",
     * "oppo",
     * "vivo",
     * "小米55"
     * ],
     * "series": [
     * {
     * "name": "手机品牌",
     * "data": [
     * 6719,
     * 5188,
     * 7265,
     * 7544,
     * 5297,
     * 7613
     * ]
     * }
     * ]
     * }
     * }
     */

    @RequestMapping("/tm")
    public String getGmvByTm(@RequestParam(value = "date", defaultValue = "0") int date,
                             @RequestParam(value = "limit", defaultValue = "5") int limit) {

        if (date == 0) {
            date = getToday();

        }
        Map gmvByTm = sugerService.getGmvByTm(date, limit);
        Set keySet = gmvByTm.keySet();
        Collection values = gmvByTm.values();

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"categories\": [\"\n" +
                StringUtils.join(keySet, "\",\"")
                +
                "\"],\n" +
                "    \"series\": [\n" +
                "      {\n" +
                "        \"name\": \"商品品牌\",\n" +
                "        \"data\": [\"\n" +
                StringUtils.join(values, "\",\"")
                +
                "\"]\n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "}";
    }


    public int getToday() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String dateTime = sdf.format(System.currentTimeMillis());
        return Integer.parseInt(dateTime);
    }
}
